package com.example.testapplication.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.testapplication.R;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.Result;
import com.example.testapplication.utils.AppConfig;
import com.example.testapplication.utils.StringUtils;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UserLoginActivity extends BaseActivity {

    private EditText userAccount;
    private EditText userPassword;
    private Button btnUserLogin;


    @Override
    protected int initLayout() {
        return R.layout.activity_user_login;
    }

    @Override
    protected void initView() {
        userAccount = findViewById(R.id.user_account);
        userPassword = findViewById(R.id.user_password);
        btnUserLogin = findViewById(R.id.btn_user_login);
    }

    @Override
    protected void initData() {
        btnUserLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = userAccount.getText().toString().trim();
                String pwd = userPassword.getText().toString().trim();
                login(account,pwd);
            }
        });
    }

    private void login(String account,String pwd) {

        if(StringUtils.isEmpty(account)){
            //Toast.makeText(this,"请输入账号",Toast.LENGTH_SHORT).show();
            showToast("请输入账号");
            return;
        }
        if(StringUtils.isEmpty(pwd)){
            //Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
            showToast("请输入密码");
            return;
        }

        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("user", account);
        params.put("password", pwd);
        Api.config(ApiConfig.USER_LOGIN,params).postRequestLogin(new CallBack() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Result res = gson.fromJson(result, Result.class);
                if(res.getCode() == 1){
                    String token = res.getData().toString();
                    saveStringToSp("token",token);
                    navigateTo(HomeActivity.class);
                    showToastAsync("登录成功");
                }else{
                    showToastAsync("登录失败");
                }
            }

            @Override
            public void onFailure(IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("no");
                    }
                });
            }
        });

//        //第一步创建OKHttpClient
//        OkHttpClient client = new OkHttpClient.Builder()
//                .build();
//        Map m = new HashMap();
//        m.put("user", account);
//        m.put("password", pwd);
//        JSONObject jsonObject = new JSONObject(m);
//        String jsonStr = jsonObject.toString();
//        RequestBody requestBodyJson =
//                RequestBody.create(MediaType.parse("application/json;charset=utf-8")
//                        , jsonStr);
//        //第三步创建Rquest
//        Request request = new Request.Builder()
//                .url(AppConfig.BASE_URL + "/user/login")
//                .addHeader("contentType", "application/json;charset=UTF-8")
//                .post(requestBodyJson)
//                .build();
//        //第四步创建call回调对象
//        final Call call = client.newCall(request);
//        //第五步发起请求
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.e("onFailure", e.getMessage());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                final String result = response.body().string();
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        showToast(result);
//                    }
//                });
//            }
//        });
    }
}